
/*
 * FormAberturaComanda.java
 *
 * Created on 05/05/2013, 23:27:14
 */
package forms;

import classes.Conexao;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Jonatas
 */
public class FormFecharComanda extends javax.swing.JInternalFrame {

    private String textoPermitido = "01234567890";
    
    /** Creates new form FormAberturaComanda */
    public FormFecharComanda() {
        initComponents();
    }
        
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jLabelComanda = new javax.swing.JLabel();
        jButtonFechar = new javax.swing.JButton();
        jTextFieldComanda = new javax.swing.JTextField(new classes.CaracteresPermitidos(textoPermitido), "", 30);
        btnPesquisar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Fechar Comanda");

        jLabelComanda.setText("Comanda:");

        jButtonFechar.setText("Fechar");
        jButtonFechar.setToolTipText("Clique aqui para fechar a comanda");
        jButtonFechar.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButtonFecharActionPerformed(evt);
            }
        });

        jTextFieldComanda.setToolTipText("Digite o número da comanda");

        btnPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/procurar.png"))); // NOI18N
        btnPesquisar.setToolTipText("Clique aqui para verificar as comandas abertas.");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnPesquisarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButtonFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelComanda)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldComanda, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(btnPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(27, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabelComanda)
                        .addComponent(jTextFieldComanda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnPesquisar))
                .addGap(20, 20, 20)
                .addComponent(jButtonFechar)
                .addGap(19, 19, 19))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-251)/2, (screenSize.height-151)/2, 251, 151);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFecharActionPerformed
        try
        {            
            //Pega o numero da comanda e converte para int
            int cac = Integer.parseInt(jTextFieldComanda.getText());
            //Pega a hora do sistema para inserir no banco
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");  
            sdf.format(new Date()); 
        
            //String sql = "INSERT INTO snooker.\"ABERTURA_COMANDA\" VALUES (abertura_comanda_seq.nextval,null,to_date('"+sdf.format(new Date())+"','dd/MM/yyyy HH24:MI'),null,"+cac+")";
            String sql = "UPDATE snooker.\"ABERTURA_COMANDA\" "
                    + "SET \"ABERTURA_COMANDA\".\"dt_hora_fechar\" = to_date('" + sdf.format(new Date()) + "', 'dd/MM/yyyy HH24:MI') "
                    + "WHERE \"ABERTURA_COMANDA\".\"cd_comanda\" = " + cac;
            
            if(Conexao.atualizar(sql) == 0) {
                JOptionPane.showMessageDialog(null, "Digite um número de alguma comanda aberta.", "Aviso", 2);
            }
            else
            {
                if(Conexao.atualizar(sql) != -1) {
                    JOptionPane.showMessageDialog(null, "Comanda " + cac + " fechada.", "Comanda", 1);
                }else{
                    JOptionPane.showMessageDialog(null, Conexao.getErro(), "Cadastro", 1);
                }
            }
            //limpa o campo da comanda
            jTextFieldComanda.setText("");
            jTextFieldComanda.requestFocus();
        }
        catch(Exception e) {
            JOptionPane.showMessageDialog(this, "Erro: \n" + e.getMessage(), "Erro!", 0);
        }
    }//GEN-LAST:event_jButtonFecharActionPerformed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnPesquisarActionPerformed
    {//GEN-HEADEREND:event_btnPesquisarActionPerformed
        FormAberturaComanda fac = new FormAberturaComanda();
        this.getDesktopPane().add(fac);
        fac.setFrameIcon(new ImageIcon(getClass().getResource("/imagens/icon.png")));
        fac.setVisible(true);
    }//GEN-LAST:event_btnPesquisarActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JButton jButtonFechar;
    private javax.swing.JLabel jLabelComanda;
    private javax.swing.JTextField jTextFieldComanda;
    // End of variables declaration//GEN-END:variables
}
